package com.kingdown.uploadfileprogressbar;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yijia.android.upload.FileDownLoadAsyncTask;
import com.yijia.android.upload.FileUploadAsyncTask;
import com.yijia.android.upload.FileUploadListener;

public class UploadFileActivity extends Activity implements FileUploadListener {

	// public static String url = "http://192.168.1.103";
	public static String url = "http://192.168.3.105";
	// 服务器地址
	private String baseUpUrl = url + "/fileupload/UploadPic.php";
	// 图片下载路径
	private String downloadUrl = url + "/fileupload/upload/";
	private String picPath;

	private ProgressBar mProgressBar;
	private TextView text_select, text_upload, text_download;
	private ImageView image_select, image_download;
	private Button btn_upload, btn_select, btn_download;

	private static final int IMAGE_REQUEST_CODE = 1110; // 请求码 本地图片

	/**
	 * 回调结果处理
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_CANCELED) {
			switch (requestCode) {
			case IMAGE_REQUEST_CODE:
				if (data != null) {
					try {
						ContentResolver resolver = this.getContentResolver();
						String[] pojo = { MediaStore.Images.Media.DATA };
						Uri uri = data.getData();
						CursorLoader cursorLoader = new CursorLoader(this, uri,
								pojo, null, null, null);
						Cursor cursor = cursorLoader.loadInBackground();
						cursor.moveToFirst();
						picPath = cursor.getString(cursor
								.getColumnIndex(pojo[0]));
						Bitmap bitmap = MediaStore.Images.Media.getBitmap(
								resolver, uri);
						if (bitmap != null) {
							image_select.setImageBitmap(bitmap);
							text_select.setText("已选择：" + picPath);
							text_upload.setText("待上传：" + picPath);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_upload_file);

		mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
		text_select = (TextView) findViewById(R.id.text_select);
		text_upload = (TextView) findViewById(R.id.text_upload);
		text_download = (TextView) findViewById(R.id.text_download);
		btn_select = (Button) findViewById(R.id.btn_select);
		btn_upload = (Button) findViewById(R.id.btn_upload);
		btn_download = (Button) findViewById(R.id.btn_download);
		image_select = (ImageView) findViewById(R.id.image_select);
		image_download = (ImageView) findViewById(R.id.image_download);

		btn_select.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(intent, IMAGE_REQUEST_CODE);
			}
		});
		btn_upload.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				File file = new File(picPath);
				if (file.exists()) {
					downloadUrl = url + "/fileupload/upload/" + file.getName();
					text_download.setText("待下载：" + downloadUrl);
					new FileUploadAsyncTask(UploadFileActivity.this,
							UploadFileActivity.this, baseUpUrl).execute(file);
				} else {
					Toast.makeText(UploadFileActivity.this, "本地文件不存在",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		btn_download.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (downloadUrl.endsWith("png") || downloadUrl.endsWith("jpg")
						|| downloadUrl.endsWith("jpeg")) {
					image_download.setTag(downloadUrl);
					new FileDownLoadAsyncTask(UploadFileActivity.this,
							UploadFileActivity.this, downloadUrl, 400, 400)
							.execute(image_download);
				} else {
					Toast.makeText(UploadFileActivity.this, "下载文件不存在",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void publishProgress(long transferedBytes, long totalSize) {
		// TODO Auto-generated method stub
		mProgressBar.setProgress((int) (100 * transferedBytes / totalSize));
	}

	@Override
	public void publishProgressShow() {
		// TODO Auto-generated method stub
		mProgressBar.setVisibility(View.VISIBLE);
	}

	@Override
	public void publishProgressDismiss() {
		// TODO Auto-generated method stub
		mProgressBar.setVisibility(View.INVISIBLE);
	}
}
